#include "Keyboard.hpp"
#include "../Global.hpp"
#include "../Tools/Matrix.hpp"
#include <cstdio> //fprintf...
#include <cstdlib> // use function exit()

#define ROT_SPEED 5.0f // angular rotation for each press
#define X_FLAG 0x01
#define Y_FLAG 0x02
#define Z_FLAG 0x03

void Keyboard(unsigned char key, int x, int y){
	static short flag = 0x00;
	switch(key){
		case 'x':
		case 'X':
			flag = X_FLAG;
#ifdef VERBOSE
			fprintf(stderr, "X axis\n");
#endif
			break;

		case 'y':
		case 'Y':
			flag = Y_FLAG;
#ifdef VERBOSE
			fprintf(stderr, "Y axis\n");
#endif
			break;

		case 'z':
		case 'Z':
			flag = Z_FLAG;
#ifdef VERBOSE
			fprintf(stderr, "Z axis\n");
#endif
			break;

		case '+':
			if(X_FLAG == flag)	
				global_rot = global_rot * Matrix::IDENTITY.Rotate(ROT_SPEED, 1.0f, 0.0f, 0.0f);
			else if(Y_FLAG == flag)
				global_rot = global_rot * Matrix::IDENTITY.Rotate(ROT_SPEED, 0.0f, 1.0f, 0.0f);
			else if(Z_FLAG == flag)
				global_rot = global_rot * Matrix::IDENTITY.Rotate(ROT_SPEED, 0.0f, 0.0f, 1.0f);
			break;

		case '-':
			if(X_FLAG == flag)
				global_rot = global_rot * Matrix::Rotate(-ROT_SPEED, 1.0f, 0.0f, 0.0f);
			else if(Y_FLAG == flag)
				global_rot = global_rot * Matrix::Rotate(-ROT_SPEED, 0.0f, 1.0f, 0.0f);
			else if(Z_FLAG == flag)
				global_rot = global_rot * Matrix::Rotate(-ROT_SPEED, 0.0f, 0.0f, 1.0f);
			break;

		case 'r':
		case 'R':
			flag = 0x00;
#ifdef VERBOSE
			fprintf(stderr, "Reset\n");
#endif
			global_rot = Matrix::IDENTITY;
			break;

		case 'q':
		case 'Q':
		case  27:
#ifdef VERBOSE
			fprintf(stderr, "Quit\n");
#endif
			exit (0);
			break;
	}
}

void KeyboardUp(unsigned char key, int x, int y){

}
